/**
 * Created by Fly on 2016/9/18.
 */
var DBPool=require('../util/DBpool');
module.exports= {
    add:function (req,res,callback){
        var needservice=req.body.needservice;
        var needscontent=req.body.needscontent;
        var needsaddress=req.body.needsaddress;
        var needsdate1=req.body.needsdate1;
        var needsdate2=req.body.needsdate2;
        var needsdate=needsdate1+'~'+needsdate2;
        var petno=req.body.petno;
        var price=req.body.price;
        var usertel=req.session.usertel;
        var fosterno=req.session.foster;
        var dealdate=new Date();
        var sql_needs="INSERT INTO t_needs(needservice,needscontent,needsaddress,needsdate,userno,petno)VALUES(?,?,?,?,?,?)";
        var sql_order="INSERT INTO t_order(fosterno,userno,price,dealdate,needsno)VALUES(?,?,?,?,?)";
        DBPool.getConnection(function (client) {
            client.query('SELECT userno FROM t_userinfo WHERE tel=?',[usertel],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                var userno=result[0].userno;
                client.query(sql_needs,[needservice,needscontent,needsaddress,needsdate,userno,petno],function (error,result) {
                    if(error){
                        console.log(error.message);
                        return;
                    }
                    client.query('SELECT needsno FROM t_needs WHERE userno=? order by needsno desc limit 0,1',[userno],function (error,result) {
                        if(error){
                            console.log(error.message);
                            return;
                        }
                        var needsno=result[0].needsno;
                        console.log("看看得到needsno是"+needsno);
                        client.query(sql_order,[fosterno,userno,price,dealdate,needsno],function (error,result) {
                        if(error){
                            console.log(error.message);
                            return;
                        }
                        callback(result);
                        client.release();
                        });
                    });
                });
            });
        });
    },
    apply:function (req,fields,a,callback){
        var useraddress=fields.user_address;
        var shop_name=fields.user_shop_name;
        var image=a[0];
        var shopcontent=fields.shop_content;
        var shopexpress=fields.shop_express;
        var cat_service=fields.cat_service;
        var dog_service=fields.dog_service;
        var mouse_service=fields.mouse_service;
        var realname=fields.realname;
        var idcard=fields.idcard;
        var idcardpic=a[1];
        var createdate=new Date();
        var usertel=req.session.usertel;
        DBPool.getConnection(function (client) {
            client.query('SELECT userno FROM t_userinfo WHERE tel=?',[usertel],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                var userno=result[0].userno;
                client.query('INSERT INTO t_image (imageobj,objID,image,createdate) VALUES (?,?,?,?)',[3,userno,image,createdate],function (error,result) {
                    if(error){
                        console.log(error.message);
                        return;
                    }
                    client.query('UPDATE t_userinfo SET usertype=?,useraddress=?,shop_name=? WHERE userno=?',[3,useraddress,shop_name,userno],function (error,result) {
                        if(error){
                            console.log(error.message);
                            return;
                        }client.query('INSERT INTO t_shop (fosterno,shopcontent,shopexpress,cat_service,dog_service,mouse_service,realname,idcard,idcardpic)VALUES (?,?,?,?,?,?,?,?,?)',[userno,shopcontent,shopexpress,cat_service,dog_service,mouse_service,realname,idcard,idcardpic],function (error,result) {
                            if(error){
                                console.log(error.message);
                                return;
                            }
                            callback(result);
                            client.release();
                        });
                    });
                });
            });
        });
    }
};
